package com.wendy.system.conf.controller;

import com.wendy.common.core.domain.TableDataInfo;
import com.wendy.common.core.utils.StringUtils;
import com.wendy.common.log.annotation.Log;
import com.wendy.common.log.enums.BusinessType;
import com.wendy.common.web.controller.BaseController;
import com.wendy.custom.security.annotation.RequiresPermissions;
import com.wendy.custom.security.utils.SecurityUtils;
import com.wendy.excel.ruoyi.utils.ExcelUtil;
import com.wendy.system.conf.domain.bo.SysDictDataBo;
import com.wendy.system.conf.domain.vo.SysDictDataExcelVo;
import com.wendy.system.conf.domain.vo.SysDictDataVo;
import com.wendy.system.conf.entity.SysDictData;
import com.wendy.system.conf.service.ISysDictDataService;
import com.wendy.system.conf.service.ISysDictTypeService;
import com.wendy.utils.spring.MapstructUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author wendy
 */
@RestController
@RequestMapping("/dict/data")
public class SysDictDataController extends BaseController {
    @Autowired
    private ISysDictDataService dictDataService;

    @Autowired
    private ISysDictTypeService dictTypeService;

    @RequiresPermissions("system:dict:list")
    @GetMapping("/page")
    public TableDataInfo page(SysDictDataBo dictData) {
        TableDataInfo<SysDictDataVo> list = dictDataService.selectDictDataPage(dictData);
        return list;
    }

    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictDataBo dictData) {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        List<SysDictDataExcelVo> result = MapstructUtils.convert(list, SysDictDataExcelVo.class);
        ExcelUtil<SysDictDataExcelVo> util = new ExcelUtil<SysDictDataExcelVo>(SysDictDataExcelVo.class);
        util.exportExcel(response, result, "字典数据");
    }

    /**
     * 查询字典数据详细
     */
    @RequiresPermissions("system:dict:query")
    @GetMapping(value = "/{dictCode}")
    public SysDictDataVo getInfo(@PathVariable Long dictCode) {
        SysDictData dic = dictDataService.selectDictDataById(dictCode);
        return MapstructUtils.convert(dic, SysDictDataVo.class);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public List<SysDictDataVo> dictType(@PathVariable String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<SysDictData>();
        }

        return MapstructUtils.convert(data, SysDictDataVo.class);
    }

    /**
     * 新增字典类型
     */
    @RequiresPermissions("system:dict:add")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public int add(@Validated @RequestBody SysDictData dict) {
        dict.setCreateBy(SecurityUtils.getUsername());
        return dictDataService.insertDictData(dict);
    }

    /**
     * 修改保存字典类型
     */
    @RequiresPermissions("system:dict:edit")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public int edit(@Validated @RequestBody SysDictData dict) {
        dict.setUpdateBy(SecurityUtils.getUsername());
        return dictDataService.updateDictData(dict);
    }

    /**
     * 删除字典类型
     */
    @RequiresPermissions("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public boolean remove(@PathVariable Long[] dictCodes) {
        dictDataService.deleteDictDataByIds(dictCodes);
        return true;
    }
}
